
<template>
  <div class="memberManger">
    <!-- 头部区域 -->
    <div class="head">
      <el-form :inline="true">
        <!-- 搜索区 -->
        <div class="headDemo1">
          <el-form-item>
            <el-input
              v-model="params.username"
              placeholder="请输入会员姓名"
              clearable
              @clear="searchUser"
            ><el-button
              slot="append"
              icon="el-icon-search"
              @click="searchUser"
            /></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="danger" @click="searchUser">查询</el-button>
          </el-form-item>
          <!-- 添加会员 -->
          <div class="headDemo2">
            <el-form-item>
              <el-button
                type="danger"
                round
                icon="el-icon-plus"
                @click="addMember"
              >添加会员</el-button>
              <!-- 模态框 -->
              <el-dialog
                v-if="dialogVisible"
                :title="dialogTitle"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose"
              >
                <!-- 表单开始 -->
                <el-form
                  ref="relForm"
                  :model="memberForm"
                  :rules="rules"
                  label-width="80px"
                >
                  <el-form-item label="会员名" prop="username">
                    <el-input v-model="memberForm.username" />
                  </el-form-item>
                  <el-form-item label="会员姓名" prop="realname">
                    <el-input v-model="memberForm.realname" />
                  </el-form-item>
                  <el-form-item label="性别">
                    <el-radio-group v-model="memberForm.gender">
                      <el-radio label="male" />
                      <el-radio label="female" />
                    </el-radio-group>
                  </el-form-item>
                  <el-form-item label="手机号码" prop="telephone">
                    <el-input v-model="memberForm.telephone" />
                  </el-form-item>
                </el-form>

                <!-- 表单结束 -->

                <span slot="footer" class="dialog-footer">
                  <el-button @click="dialogVisible = false">取 消</el-button>
                  <el-button
                    type="primary"
                    @click="submitMemberForm"
                  >确 定</el-button>
                </span>
              </el-dialog>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>

    <!-- 表格区 -->
    <!-- <div class="content"> -->
    <el-table :data="memberData.list" border stripe>
      <el-table-column
        type="index"
        :index="1"
        label="序号"
        align="center"
      />

      <el-table-column prop="username" label="会员名" align="center" />

      <el-table-column prop="realname" label="真实姓名" align="center" />

      <el-table-column prop="gender" label="性别" align="center" />

      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == '正常'" type="success">正常</el-tag>
          <el-tag v-else type="danger" />
        </template>
      </el-table-column>

      <el-table-column prop="telephone" label="手机号" align="center" />

      <el-table-column fixed="right" label="操作" width="180px" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="deleteInfo(scope.row.id)"
          >删除</el-button>
          <el-button
            type="text"
            size="small"
            @click="editUser(scope.row)"
          >编辑</el-button>
          <el-button
            type="text"
            size="small"
            @click="moreInfo(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- </div> -->

    <!--分页区域-->
    <div class="pagination">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="10"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from 'vuex'
import { get, post, del } from '@/utils/request'
export default {
  data() {
    // 验证手机号的规则;
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile =
        /^(0|86|17951)?(13[0-9]|15[0123456789]|17[678]|18[0-9]|14[57])[0-9]{8}$/

      if (regMobile.test(value)) {
        return cb()
      }
      cb(new Error('请输入合法的电话'))
    }
    return {
      // form: {},
      // tableData: [],
      params: {
        page: 1,
        pageSize: 10,
        username: ''
      },
      // 控制添加用户对话框的显示与隐藏
      addDialogVisible: false,
      // 模态框的标题
      dialogTitle: '',
      // 控制模态框的显示与隐藏
      dialogVisible: false,
      // 表单对象 要传递给后端的对象
      memberForm: {
        // 会员名
        username: '',
        // 会员姓名
        realname: '',
        // 手机号
        telephone: '',
        gender: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入会员名', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '会员名的长度在1~10个字符之间',
            trigger: 'blur'
          }
        ],
        realname: [
          { required: true, message: '请输入真实姓名', trigger: 'blur' },
          {
            min: 1,
            max: 10,
            message: '用户名的长度在1~10个字符之间',
            trigger: 'blur'
          }
        ],
        telephone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { validator: checkMobile, trigger: 'blur' }
        ]
      }
    }
  },
  // 组件方法
  methods: {
    moreInfo(row) {
      this.$router.push({
        path: 'InfoDetails',
        query: row
      })
    },
    ...mapActions('member', ['getMemberDataVuex']),
    editUser(userObj) {
      // 模态框弹出
      this.dialogVisible = true
      // 对应的对象进行一个赋值
      this.memberForm = { ...userObj }
    },
    getMemberData() {
      // this.loading = true;
      // 设置参数
      const param = {
        page: this.params.page,
        pageSize: this.params.pageSize,
        username: this.params.username
      }
      // 调用vuex中对应的方法
      this.getMemberDataVuex(param)
    },
    // 搜索用户
    searchUser() {
      this.getMemberData()
    },
    // 监听pagesize改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize);
      this.params.pageSize = newSize
      this.getMemberData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.params.page = newPage
      this.getMemberData()
    },
    // 新增会员
    addMember() {
      // 3.清空表单对象中的数据 数据遗留的问题
      this.memberForm = {}
      // 1.修改模态框的标题
      this.dialogTitle = '添加会员'
      // 2.弹出模态框
      this.dialogVisible = true
    },
    // 关闭模态框之前要做一些处理
    handleClose() {
      this.$confirm('确认关闭？')
        // 确认按钮事件
        .then((_) => {
          // 清空表单的数据 和表单验证的信息
          this.$refs['relForm'].resetFields()
          this.dialogVisible = false
          done()
        })
        // 取消按钮事件
        .catch((_) => {})
    },
    // 保存或更新产品信息
    submitMemberForm() {
      this.$refs['relForm'].validate((valid) => {
        if (valid) {
          post('/baseUser/saveOrUpdate', this.memberForm).then((res) => {
            // alert("submit!");
            // 判断状态码
            if (res.status == 200) {
              this.$notify({
                title: '成功',
                message: res.message,
                type: 'success'
              })
              // 刷新页面数据
              this.getMemberData()
              // this.loading = false;
              // 关闭模态框
              this.dialogVisible = false
            } else {
              this.$notify({
                title: '失败',
                message: res.message,
                type: 'danger'
              })
            }
          })
        } else {
          // console.log("error submit!!");
          return false
        }
      })
    },
    // 根据id删除用户
    deleteInfo(id) {
      this.$confirm('此操作将永久删除该用户信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          get('/baseUser/deleteById', { id: id }).then((res) => {
            if (res.message == '删除成功') {
              this.$message({
                message: res.message,
                type: 'success'
              })

              this.getMemberData({
                page: this.page,
                pageSize: this.pageSize
              })
            } else {
              this.$message({
                type: 'error'
              })
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  },
  created() {
    // this.findAllMembers();
    this.getMemberData()
  },
  computed: {
    // 声明辅助函数 获取state中的值
    ...mapState('member', ['memberData'])
  }
}
</script>

<style scoped>
.memberManger {
  /* 设置一个最小高度 vh视窗高度*/
  min-height: calc(100vh - 80px);
}
.headDemo2 {
  float: right;
}
.pagination {
  position: fixed;
  right: 20px;
  bottom: 30px;
}
</style>
